package com.power.sifa.correction.model.pojo.bo.rule;

import cn.hutool.core.util.ObjectUtil;
import com.power.sifa.correction.model.pojo.bo.CheckRuleRequestBO;
import com.power.sifa.correction.model.pojo.bo.CheckRuleResultBO;
import lombok.Data;

import java.util.List;
import java.util.function.Supplier;

/**
 * @ClassName AbstractRuleHandler
 * @Author xiongzw
 * @Date 2023/12/10 10:17
 * @Description 规则抽象处理类
 * @Version: 1.0
 **/

@Data
public abstract class AbstractRuleHandler implements IRuleHandler {

    private IRuleHandler nextRule;

    @Override
    public IRuleHandler getNextRule() {
        return nextRule;
    }

    /**
     * 设置下一规则
     * @param
     * @return
     */
    @Override
    public void setNextRule(Supplier<IRuleHandler> supplier) {
        this.nextRule = supplier.get();
    }


    @Override
    public void initNextRule(Supplier<IRuleHandler> supplier) {
        if (ObjectUtil.isNotNull(this.nextRule)) {
            IRuleHandler tempNextRule = this.nextRule;
            do {
                tempNextRule = ObjectUtil.isNotNull(tempNextRule.getNextRule()) ? tempNextRule.getNextRule(): tempNextRule;
            } while(ObjectUtil.isNotNull(tempNextRule) && ObjectUtil.isNotNull(tempNextRule.getNextRule()));
            tempNextRule.setNextRule(supplier);
        } else {
            this.nextRule = supplier.get();
        }
    }

    /**
     * 校验当前规则
     * @param request 请求对象
     * @param checkRuleResultList 结果对象
     */
    @Override
    public abstract void handleRule(CheckRuleRequestBO request, List<CheckRuleResultBO> checkRuleResultList);

}
